package com.plum.admin.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 子丶沫
 * @date 2021-06-23 15:52
 */
@Component
@Slf4j
public class IpUtil {
    public Map<String,String>  getLocation(String key){
        String url="https://apis.map.qq.com/ws/location/v1/ip?key=1";
        Map<String,String> param= Maps.newHashMap();
        url=url.replaceAll("key=1","key="+key);
        String data = HttpUtil.get(url);
        JSONObject object = JSONObject.parseObject(data);
        if(object.getInteger("status")==0){
            JSONObject result = object.getJSONObject("result");
            JSONObject adInfo = result.getJSONObject("ad_info");
            param.put("location",adInfo.getString("nation")+"-"+adInfo.getString("province")+"-"+adInfo.getString("city"));
            param.put("ip",result.getString("ip"));
        }
        return param;
    }
    public String getIpAddr() {
        String ip = "";
        String chinaz = "http://ip.chinaz.com";
        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
        }
        return ip;
    }
    public String getIpAddr(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if ("127.0.0.1".equals(ip)) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                    ip = inet.getHostAddress();
                } catch (UnknownHostException e) {
                    log.warn(e.toString());
                }
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        return ip;
    }
    public JSONObject getLocation(HttpServletRequest request){
        String ipAddr = getIpAddr();
        String url= "https://api.map.baidu.com/location/ip?ak=QQUqrbWQZzaAec02tkB4oKuUuwS5GrtK&ip=" +ipAddr+"&coor=bd09ll";
        String content = HttpUtil.get(url);
        JSONObject object = JSONObject.parseObject(content);
        JSONObject obj = object.getJSONObject("content");
        return obj.getJSONObject("point");
    }
}
